//
//  PlayDetailViewController.m
//  TravelEveryDay
//
//  Created by 路洪宇 on 15/10/16.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "PlayDetailViewController.h"
#import "UMSocial.h"
#import "DataBase.h"
@interface PlayDetailViewController ()<UIWebViewDelegate,UMSocialUIDelegate>

{
    UIActivityIndicatorView *activity;
    NSInteger _counta;
}
@end

@implementation PlayDetailViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets =NO;
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.myNewDataArr = [[NSMutableArray alloc]init];

    [self setUpTitleView];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    _webView.delegate =self;
    _webView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:[NSURL  URLWithString:[NSString stringWithFormat:@"http://web.breadtrip.com/hunter/product/%@/", self.tempModel.product_id]]];
    
    [_webView  loadRequest:request];
    [self.view  addSubview:_webView];
    //创建小菊花
    activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activity.center =self.view.center;
    [self.view addSubview:activity];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick :)];
    
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    
    [_webView addGestureRecognizer:swipe];
}
-(void)setUpTitleView
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [titleView setBackgroundColor:TITLECOLOR];
    [self.view addSubview:titleView];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [backButton setFrame:CGRectMake(10, 30, 22, 22)];
    [backButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:backButton];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 25, 200, 30)];
    [title setText:@"体验详情"];
    [title setTextColor:[UIColor whiteColor]];
    title.font = [UIFont systemFontOfSize:19.];
    title.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:title];
    
    _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_shareButton setFrame:CGRectMake(SCREEN_WIDTH-40, 30, 20, 20)];
    [_shareButton setImage:[UIImage imageNamed:@"play-fenxiang.png"]forState:UIControlStateNormal];
    [_shareButton addTarget:self action:@selector(shareClick) forControlEvents:UIControlEventTouchUpInside];

    [titleView addSubview:_shareButton];
    
    _collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_collectButton setFrame:CGRectMake(SCREEN_WIDTH-75, 32, 18, 18)];
    [_collectButton setImage:[UIImage imageNamed:@"play-unlike.png"]forState:UIControlStateNormal];
    [_collectButton setImage:[UIImage imageNamed:@"play-like.png"]forState:UIControlStateSelected];
    [_collectButton addTarget:self action:@selector(collectClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _collectButton.selected = NO;
    for (PlayModel *model in [[DataBase shareDataBase]selectAll]) {
        if ([self.tempModel.title isEqualToString:model.title]) {
            _collectButton.selected = YES;
        }
    }
    
    [titleView addSubview:_collectButton];
    [self.view addSubview:titleView];
}
-(void)leftClick
{
#pragma mark- 返回指定页面
    UIViewController *vc =[self.navigationController.viewControllers objectAtIndex:0];
    [self.navigationController popToViewController:vc animated:YES];
    self.tabBarController.tabBar.hidden = NO;


}
#pragma mark-滑动手势
-(void)swipeClick:(UISwipeGestureRecognizer *)swipe
{
    UIViewController *vc =[self.navigationController.viewControllers objectAtIndex:0];
    [self.navigationController popToViewController:vc animated:YES];
    self.tabBarController.tabBar.hidden = NO;

}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [activity startAnimating];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activity stopAnimating];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [activity stopAnimating];
}



-(void)collectClick:(UIButton *)sender
{
    _counta = 0;
    NSMutableArray *arraya = [[DataBase shareDataBase]selectAll];
    for (PlayModel *modela in arraya) {
        if ([modela.title isEqualToString:self.tempModel.title]) {
            _counta ++;
        }
    }
    if (_counta == 0) {
        [[DataBase shareDataBase]insertStudent:self.tempModel];
    }
    if (!sender.selected) {
        sender.selected = YES;
    }else{
        [[DataBase shareDataBase]deleteStudent:self.tempModel.title];
        sender.selected = NO;
    }
}


-(void)viewDidDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;

}

-(void)shareClick
{
    NSString *url = [NSString stringWithFormat:@"http://web.breadtrip.com/hunter/product/%@/",self.tempModel.product_id];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"507fcab25270157b37000010"
                                      shareText:[NSString stringWithFormat:@"「%@」%@",self.tempModel.title,url]
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
